package com.ktjy.entity;

import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * pojo与entity都表示实体类的包
 * 用户实体类，它与sys_user对应
 * @version v1.0
 * @Author JieGe
 * @Create 2024-11-12 - 10:01
 */
public class SysUser {

    private Integer id;
    private String account;  // 账户/账号
    private String realName; // 用户的真实姓名
    private String password;
    private Integer sex;
    // 将注解，实现将字符串转换成日期类型再赋值给下面属性
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date birthday; // 导入的是java.util.Date包
    private Integer age;  // 用来保存计算好年龄
    private String phone; // 手机号码
    private String address;  // 住址
    private String idPicPath;  // 证件照
    private String workPicPath;  //  工作照
    private Integer roleId; // 角色id
    private Integer createdUserId;
    private Date createdTime;
    private Integer updatedUserId;
    private Date updatedTime;
    private String userRoleName;//角色名称(在sys_user用户表中并没有userRoleName的列名)

    // 用户类内部“有一个”用户角色类型的属性
    private SysRole sysRole;  // SysRole角色类型的属性，角色对象

    // 一个用户拥有多个地址列表
    private List<Address> addressList;

    public String getIdPicPath() {
        return idPicPath;
    }

    public void setIdPicPath(String idPicPath) {
        this.idPicPath = idPicPath;
    }

    public String getWorkPicPath() {
        return workPicPath;
    }

    public void setWorkPicPath(String workPicPath) {
        this.workPicPath = workPicPath;
    }

    // 根据用户的出生日期计算该用户的年龄
    public int getAge() {
        if (this.birthday == null) {
            return 0;  // 当birthday为null，默认年龄为0
        }
        // 接下来计算年龄
        // 将Date转换成LocalDate类型
        LocalDate bithLocalDate = birthday.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        // 再得到LocalDate类型的当前时间
        LocalDate currDate = LocalDate.now();
        // 再计算当前时间与出生日期相隔的年份   getYears()得到两个时间差的年份
        return Period.between(bithLocalDate, currDate).getYears();
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<Address> getAddressList() {
        return addressList;
    }

    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }

    public SysRole getSysRole() {
        return sysRole;
    }

    public void setSysRole(SysRole sysRole) {
        this.sysRole = sysRole;
    }

    public String getUserRoleName() {
        return userRoleName;
    }

    public void setUserRoleName(String userRoleName) {
        this.userRoleName = userRoleName;
    }

    @Override
    public String toString() {
        return "SysUser{" +
                "id=" + id +
                ", account='" + account + '\'' +
                ", realName='" + realName + '\'' +
                ", password='" + password + '\'' +
                ", sex=" + sex +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", roleId=" + roleId +
                ", createdUserId=" + createdUserId +
                ", createdTime=" + createdTime +
                ", updatedUserId=" + updatedUserId +
                ", updatedTime=" + updatedTime +
                ", userRoleName='" + userRoleName + '\'' +
                // ", sysrole='" + sysRole.toString() + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(Integer roleId) {
        this.roleId = roleId;
    }

    public Integer getCreatedUserId() {
        return createdUserId;
    }

    public void setCreatedUserId(Integer createdUserId) {
        this.createdUserId = createdUserId;
    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Integer getUpdatedUserId() {
        return updatedUserId;
    }

    public void setUpdatedUserId(Integer updatedUserId) {
        this.updatedUserId = updatedUserId;
    }

    public Date getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(Date updatedTime) {
        this.updatedTime = updatedTime;
    }
}
